import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/views/layout/LayoutIndex.vue'
import Login from '@/views/login/LoginIndex.vue'
//异步
const Home =()=>import('@/views/Home/HomeIndex.vue')//首页
const Goods =()=>import('@/views/Goods/GoodsIndex.vue')
const Params =()=>import('@/views/Params/ParamsIndex.vue')
const SpecificaTions =()=>import('@/views/Params/ParamsInfo/SpecificaTions.vue')
const Advert =()=>import('@/views/Advert/AdvertIndex.vue')

const Order =()=>import('@/views/Order/OrderIndex.vue')
const OrderList = ()=>import("../views/Order/OrderList/OrderList.vue")
const OrderBack = ()=>import("../views/Order/OrderBack/OrderBack.vue")

const UserIndex =()=>import('../views/User/UserIndex.vue')

// 子级路由
const AddGoods =()=>import('@/views/Goods/AddGoods.vue')

//点击跳转到同一路径
//vuerouter上配置路由跳转 在router中index.js上配置 注意在use之前
// 保存原始的 VueRouter.prototype.push 方法
const routerPush = VueRouter.prototype.push;
// 重写 VueRouter.prototype.push 方法
VueRouter.prototype.push = function(location) {
    // 调用原始的 push 方法，并捕获可能出现的错误
    return routerPush.call(this, location).catch(err => {
        // 如果错误是重复路由跳转导致的，不做处理
        if (err.name !== 'NavigationDuplicated') {
            // 若不是重复路由跳转错误，将错误抛出
            throw err;
        }
    });
};

// const routerPush = VueRouter.prototype.push;
// VueRouter.prototype = function(location){
//   return routerPush.call(this,location).catch(err=>{})
// },
Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    name:'',
    component:Layout,
    //路由的原信息
    meta:{isLogin:true},
    children:[
      {
        path:'/home',
        name:'home',
        component:Home,
        meta:{
          title:'首页'
        }
      },
      {
        path:'/goods',
        name:'goods',
        component:Goods,
        meta:{
          title:'商品管理'
        }
      },
      {
        path:'/add-goods',
        name:'addgoods',
        component:AddGoods,
      },//用不上
      {
        path:'/params',
        name:'params',
        component:Params,
        meta:{
          title:'参数规格'
        },
        redirect:'/params/SpecificaTions',
        children:[
          {
            path:'SpecificaTions',
            name:'SpecificaTions',
            component:SpecificaTions,
            meta:{
                title:'订单列表'
            }
          }
        ]
      },
      {
        path:'/advert',
        name:'advert',
        component:Advert,
        meta:{
          title:'广告分类'
        },
      },

      {
        path:'/order',
        name:'order',
        component:Order,
        meta:{
          title:'订单管理'
        },
        redirect:'/order/order-list',//重定向
        children:[
          {
            path:'order-list',
            component:OrderList,
            meta:{
              title:'订单列表'
            },
          },
          {
            path:'order-back',
            name:'order-back',
            component:OrderBack,
            meta:{
              title:'退货'
            },
          },

        ]
      },
      {
        path:'/user',
        name:'UserIndex',
        component:UserIndex,
        meta:{
          title:'个人中心'
        }
      },
    ]
  },
  {
    path:'/login',
    name:'',
    component:Login
  },

]

const router = new VueRouter({
  routes
})

export default router
